import { Redirect } from "react-router-dom"
import { lazy } from 'react'
import Home from "@/pages/home/Home"
import Index from "@/pages/index/Index"
import Zhuozi from "@/pages/goods/jiaju/Zhuozi"
import Classity from "@/pages/goods/Classity"
var _import = (page) => lazy(() => import(`@/pages/${page}`))
const router = {
  basename: "/",
  routes: [
    {
      path: "/index",
      component: Home, //_import("home/Home"),
      // exact: true,

      children: [
        {
          path: "/index",
          component: Index,// _import("index/Index"),
          exact: true,
          meta: {
            title: "首页"
          }
        },

        {
          path: "/index/goods",
          component: _import("goods/Index"),
          meta: {
            title: "商品管理"

          },
          children: [
            {
              path: "/index/goods",
              component: _import("goods/Gl"),
              exact: true,

              meta: {
                title: "商品管理",
                requiresAuth: true
              }
            },
            {
              path: "/index/goods/classity",
              component: Classity,//_import("goods/Classity"),
              meta: {
                title: "商品类目",
                requiresAuth: true
              }
            },
            {
              path: "/index/goods/jiaju",
              component: _import("goods/jiaju/JiaJu"),
              children: [
                {
                  path: "/index/goods/jiaju/yizi",
                  component: _import("goods/jiaju/Yizi"),
                  meta: {
                    title: "椅子",
                    requiresAuth: true
                  },
                },
                {
                  path: "/index/goods/jiaju/zhuozi",
                  component: Zhuozi, //_import("goods/jiaju/Zhuozi"),
                  meta: {
                    title: "桌子",
                    requiresAuth: true
                  },
                }
              ]
            },
            //  { path:"*", exact: true, render: () => <div>404 goods</div>  },

          ]

        },
        // { path:"*", exact: true, render: () => <div>404 index</div>  },

      ],
    },
    {
      path: "/myHook",
      component: _import("myHook/MyHook"),
      // exact: true,
      meta: {
        title: "myHook"
      }
    },
    {
      path: "/login",
      component: _import("login/Login"),
      meta: {
        title: "登录",
      },
    },
    {
      path: "/",
      exact: true,
      render: () => <Redirect to="/index"></Redirect>
    },
    { path: "/404", exact: true, render: () => <div>404</div> },
    // {  exact: true, component: () => <div>404</div> },
  ],

}

router.beforeEach = (to, form, history) => {
  console.log(to)
  console.log(form)
  console.log(history)
  if (to.meta.title) {
    document.title = to.meta.title
  }
  let token = localStorage.getItem("token")
  if (to.meta.requiresAuth && !token) {
    history.replace("/login")
  }
}
export default router